package com.example.service.impl;


import com.example.mapper.ShowBusinessMapper;
import com.example.service.ShowBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;

/**
 * @program: cloud_market
 * @description: 短信业务展示实现层
 * @author: ChenZhiXiang
 * @create: 2019-08-09 15:57
 **/
@Service
public class ShowBusinessServiceImpl implements ShowBusinessService {

    private static final SimpleDateFormat YYYY_MM = new SimpleDateFormat("yyyy-MM");
    private static final SimpleDateFormat YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");



    @Autowired
    private ShowBusinessMapper showBusinessMapper;

    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Override
    public Object findSmsCount(String orgId) throws Exception {

        long cur = System.currentTimeMillis();
        System.out.println(cur);
        // 今日发送短信量
        String today = YYYY_MM_DD.format(new Date());
        int todaySent = showBusinessMapper.findSmsCount(today,orgId);

        // 累计发送量
        int addUpSent = showBusinessMapper.findSmsCount(null,orgId);

        // 该企业的套餐量
        int comboNum = showBusinessMapper.findComboNum(orgId);
        String currentMonth = YYYY_MM.format(new Date());
        // 当月的发送量
        int monthSent = showBusinessMapper.findSmsCount(currentMonth,orgId);
        // 套餐剩余量 = 套餐量 - 当月发送量
        int comboResidue = (comboNum-monthSent) > 0 ? (comboNum-monthSent) : 0;

        // 套餐额外使用量
        int comboOuter = (comboNum-monthSent) > 0 ? 0 : (monthSent - comboNum);

        Map<String,Object> map = new HashMap<>();
        map.put("todaySent",todaySent);
        map.put("addUpSent",addUpSent);
        map.put("comboResidue",comboResidue);
        map.put("comboOuter",comboOuter);
        long end = System.currentTimeMillis();
        System.out.println("耗时"+(end-cur));
        return map;
    }

    @Override
    public Object findSmsCoun(String orgId) throws Exception {

        // 多线程使用该集合
        ConcurrentMap<String,Object> map = new ConcurrentHashMap<>();

        System.out.println("-----------------主线程开始"+Thread.currentThread()+"-----------------------");
        long cur = System.currentTimeMillis();
        System.out.println(cur);

        //初始化数字是子线程数
        CountDownLatch countDownLatch = new CountDownLatch(4);

        taskExecutor.execute(() -> {
            try {
                System.out.println("【今日发送短信量】"+Thread.currentThread());
                // 今日发送短信量
                String today = YYYY_MM_DD.format(new Date());
                int todaySent = showBusinessMapper.findSmsCount(today,orgId);
                map.put("todaySent",todaySent);
            }finally {
                if (null != countDownLatch){
                    countDownLatch.countDown();
                }
            }

        });

        taskExecutor.execute(() -> {
            try {
                System.out.println("【累计发送量】"+Thread.currentThread());
                // 累计发送量
                int addUpSent = showBusinessMapper.findSmsCount(null,orgId);
                map.put("addUpSent",addUpSent);
            }finally {
                if (null != countDownLatch){
                    countDownLatch.countDown();
                }
            }
        });

        taskExecutor.execute(() -> {
            try {
                System.out.println("【该企业的套餐量】"+Thread.currentThread());
                // 该企业的套餐量
                int comboNum = showBusinessMapper.findComboNum(orgId);
                String currentMonth = YYYY_MM.format(new Date());
                // 当月的发送量
                int monthSent = showBusinessMapper.findSmsCount(currentMonth,orgId);
                // 套餐剩余量 = 套餐量 - 当月发送量
                int comboResidue = (comboNum-monthSent) > 0 ? (comboNum-monthSent) : 0;
                // 套餐额外使用量
                int comboOuter = (comboNum-monthSent) > 0 ? 0 : (monthSent - comboNum);
                map.put("comboResidue",comboResidue);
                map.put("comboOuter",comboOuter);
            }finally {
                if (null != countDownLatch){
                    countDownLatch.countDown();
                }
            }
        });

        taskExecutor.execute(() -> {
            try {
                System.out.println("hahahhhahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
            }finally {
                if (null != countDownLatch){
                    countDownLatch.countDown();
                }
            }
        });
        countDownLatch.await();
        long end = System.currentTimeMillis();
        System.out.println("耗时"+(end-cur));
        System.out.println("-----------------主线程结束"+Thread.currentThread()+"-----------------------");
        return map;
    }

    @Override
    public Object findSmsCou(String orgId) throws Exception {

        // 多线程使用该集合
        ConcurrentMap<String,Object> map = new ConcurrentHashMap<>();

        System.out.println("-----------------主线程开始"+Thread.currentThread()+"-----------------------");
        long cur = System.currentTimeMillis();
        System.out.println(cur);

        taskExecutor.execute(() -> {
            System.out.println("【今日发送短信量】" + Thread.currentThread());
            // 今日发送短信量
            String today = YYYY_MM_DD.format(new Date());
            int todaySent = showBusinessMapper.findSmsCount(today, orgId);
            map.put("todaySent", todaySent);
            try {
                Thread.currentThread().join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        taskExecutor.execute(() -> {
            System.out.println("【累计发送量】"+Thread.currentThread());
            // 累计发送量
            int addUpSent = showBusinessMapper.findSmsCount(null,orgId);
            map.put("addUpSent",addUpSent);
            try {
                Thread.currentThread().join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        taskExecutor.execute(() -> {

            System.out.println("【该企业的套餐量】"+Thread.currentThread());
            // 该企业的套餐量
            int comboNum = showBusinessMapper.findComboNum(orgId);
            String currentMonth = YYYY_MM.format(new Date());
            // 当月的发送量
            int monthSent = showBusinessMapper.findSmsCount(currentMonth,orgId);
            // 套餐剩余量 = 套餐量 - 当月发送量
            int comboResidue = (comboNum-monthSent) > 0 ? (comboNum-monthSent) : 0;
            // 套餐额外使用量
            int comboOuter = (comboNum-monthSent) > 0 ? 0 : (monthSent - comboNum);
            map.put("comboResidue",comboResidue);
            map.put("comboOuter",comboOuter);
            try {
                Thread.currentThread().join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        long end = System.currentTimeMillis();
        System.out.println("耗时"+(end-cur));
        System.out.println("-----------------主线程结束"+Thread.currentThread()+"-----------------------");
        return map;
    }
}
